const router = require('koa-router')()

const bot = require('../bot/index');

router.prefix('/bot')

router.get('/', async (ctx, next) => {
  let qr = ""
  let logined = false
  let botInfo;
  let botLogonnoff = bot.getLogonoff()
  let status = bot.getStatus();
  console.info("bot logined", status)
  if (botLogonnoff){
    logined = true
    botInfo = bot.getBotInfo()
  } else {
    if (status == "NOT_LOGINED"||status == "START_ERROR"||status == "LOGOUT"){
      await bot.start()
      console.info("qrcoce=============", qr)
    } else if (status=="SCANING"){
      qr = bot.getQrCode()
    }
  }

  await ctx.render('bot',  { 
    logined: logined,
    status: status,
    qr: qr,
    botInfo: botInfo
  })
})

router.get('/bar', function (ctx, next) {
  ctx.body = 'this is a users/bar response'
})

module.exports = router
